//
//  PAWFLoadingSwitch.m
//  PAWFLoadingSwitch
//
//  Created by lixinxing on 16/6/23.
//  Copyright © 2016年 Ping An Insurance(Group) Company of China, Ltd. All rights reserved.
//

#import "PAWFLoadingSwitch.h"

#define PAWF_LoadingSwitch_Margin 2

@interface PAWFLoadingSwitch ()

//滑块
@property (strong, nonatomic) UIView * dotView;

@property (strong, nonatomic) UIActivityIndicatorView * activityView;
@property (copy, nonatomic) PAWFLoadingSwitchClick clickBlock;
@end

@implementation PAWFLoadingSwitch

#pragma mark - Life cycle
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void) commonInit {
    _switchType = -1;
    
    self.layer.cornerRadius = CGRectGetHeight(self.frame) / 2.0;
    self.clipsToBounds = YES;
    [self addTarget:self action:@selector(didClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.dotView];
    [self addSubview:self.activityView];
    
    self.switchType = PAWFLoadingSwitchOff;
}

#pragma mark - Setter and getter
- (UIView *)dotView {
    if (!_dotView) {
        CGFloat size = CGRectGetHeight(self.frame) - PAWF_LoadingSwitch_Margin * 2;
        _dotView = [[UIView alloc] initWithFrame:CGRectMake(PAWF_LoadingSwitch_Margin, PAWF_LoadingSwitch_Margin, size, size)];
        _dotView.backgroundColor = [UIColor whiteColor];
        _dotView.layer.cornerRadius = size / 2;
        _dotView.clipsToBounds = YES;
        _dotView.userInteractionEnabled = NO;
    }
    return _dotView;
}

- (UIActivityIndicatorView *)activityView {
    if (!_activityView) {
        UIActivityIndicatorView * actView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _activityView.hidesWhenStopped = YES;
        _activityView = actView;
        _activityView.userInteractionEnabled = NO;
    }
    return _activityView;
}

- (void)setSwitchType:(PAWFLoadingSwitchType)switchType {
    
    if (_switchType == switchType) {
        return;
    }
    _switchType = switchType;
    
    switch (_switchType) {
        case PAWFLoadingSwitchOn:
            [self setSwitchTypeForOn];
            break;
        case PAWFLoadingSwitchLoading:
            [self setSwitchTypeForLoading];
            break;
        case PAWFLoadingSwitchOff:
            [self setSwitchTypeForOff];
            break;
        default:
            NSLog(@"错误的PAWFLoadingSwitchType类型");
            break;
    }
}

#pragma mark - Private method
- (void) setSwitchTypeForOff {
    CGFloat size = CGRectGetHeight(self.frame) - PAWF_LoadingSwitch_Margin * 2;
    CGRect rect = CGRectMake(PAWF_LoadingSwitch_Margin, PAWF_LoadingSwitch_Margin, size, size);
    [UIView animateWithDuration:0.25 animations:^{
        _dotView.frame = rect;
        _dotView.hidden = NO;
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.3];
        _dotView.backgroundColor = [UIColor whiteColor];
    } completion:^(BOOL finished) {
        [_activityView stopAnimating];
    }];
   
}

- (void) setSwitchTypeForLoading {
    self.backgroundColor = [UIColor whiteColor];
    _dotView.hidden = YES;
    [_activityView startAnimating];
}

- (void) setSwitchTypeForOn {
    
    CGFloat size = CGRectGetHeight(self.frame) - PAWF_LoadingSwitch_Margin * 2;
    CGFloat x = CGRectGetWidth(self.frame) - size - PAWF_LoadingSwitch_Margin;
    CGRect rect = CGRectMake(x, PAWF_LoadingSwitch_Margin, size, size);
    
    [UIView animateWithDuration:0.25 animations:^{
        _dotView.hidden = NO;
        _dotView.frame = rect;
        _dotView.backgroundColor = [UIColor colorWithRed:34 / 255.0 green:200  / 255.0 blue:159  / 255.0 alpha:1];
        self.backgroundColor = [UIColor whiteColor];
    }];

    [_activityView stopAnimating];
}

#pragma mark - Event response
- (void)didClick {
    if (self.clickBlock) {
        self.clickBlock(self, self.switchType);
    }
}

#pragma mark - Public method
- (void) handleSwithClick:(PAWFLoadingSwitchClick)clickBlock {
    self.clickBlock = clickBlock;
}

#pragma mark - Override method
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.layer.cornerRadius = CGRectGetHeight(self.frame) / 2.0;
    
    CGRect dotRect = CGRectZero;
    CGFloat size = CGRectGetHeight(self.frame) - PAWF_LoadingSwitch_Margin * 2;
    if (_switchType == PAWFLoadingSwitchOn || _switchType == PAWFLoadingSwitchLoading) {
        CGFloat x = CGRectGetWidth(self.frame) - size - PAWF_LoadingSwitch_Margin;
        dotRect = CGRectMake(x, PAWF_LoadingSwitch_Margin, size, size);
    }
    else {
        dotRect = CGRectMake(PAWF_LoadingSwitch_Margin, PAWF_LoadingSwitch_Margin, size, size);
    }
    _dotView.frame = dotRect;
    _dotView.layer.cornerRadius = size / 2.0;
    
    CGPoint center = CGPointMake(CGRectGetWidth(self.frame) / 2.0, CGRectGetHeight(self.frame) / 2.0);
    _activityView.center = center;
}

@end
